﻿using Microsoft.Media.CaptureClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage;

namespace Microsoft.Media.CaptureClient.Sample
{
    public class Preferences
    {
        const string Key_UsageCount = "UsageCount";
        const string Key_IngestUrl = "IngestUrl";
        const string Key_IngestStream = "IngestStream";
        const string Key_VideoEncodingPreferences = "VideoEncodingPreferences";
        const string Key_PreviewUrl = "PreviewUrl";
        const string Key_AudioDeviceId = "AudioDeviceId";
        const string Key_VideoDeviceId = "VideoDeviceId";

        private Preferences()
        {
            AreChangesAllowed = true;
        }

        static Preferences current;
        public static Preferences Current
        {
            get
            {
                return current ?? (current = new Preferences());
            }
        }

        public event EventHandler VideoEncodingPreferencesChanged;
        public event EventHandler AudioDeviceIdChanged;
        public event EventHandler VideoDeviceIdChanged;

        public bool AreChangesAllowed { get; set; }

        public VideoEncodingPreferences VideoEncodingPreferences
        {
            get
            {
                object oMultiBitrate;
                if (ApplicationData.Current.LocalSettings.Values.TryGetValue(Key_VideoEncodingPreferences, out oMultiBitrate))
                {
                    return (VideoEncodingPreferences)(int)oMultiBitrate;
                }
                else
                {
#if ARM || WINDOWS_PHONE_APP
                    return VideoEncodingPreferences.SingleBitrate;
#else
                    return VideoEncodingPreferences.MultiResolution;
#endif
                }
            }
            set
            {
                if (VideoEncodingPreferences != value)
                {
                    ApplicationData.Current.LocalSettings.Values[Key_VideoEncodingPreferences] = (int)value;
                    if (VideoEncodingPreferencesChanged != null) VideoEncodingPreferencesChanged(this, EventArgs.Empty);
                }
            }
        }

        public int UsageCount
        {
            get
            {
                int result = 0;
                object oUsageCount;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_UsageCount, out oUsageCount))
                {
                    result = (int)oUsageCount;
                }
                return result;
            }
            private set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_UsageCount] = value;
            }
        }

        public void IncrementUsageCount()
        {
            UsageCount++;
        }

        public string StreamId
        {
            get
            {
                object oStreamId;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_IngestStream, out oStreamId))
                {
                    return (string)oStreamId;
                }
                else return "Encoder1";
            }
            set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_IngestStream] = value;
            }
        }

        public string IngestUrl
        {
            get
            {
                object oIngestUrl;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_IngestUrl, out oIngestUrl))
                {
                    return (string)oIngestUrl;
                }
                else return "http://channelname.accountname.channel.mediaservices.windows.net/ingest.isml";
            }
            set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_IngestUrl] = value;
            }
        }

        public string PreviewUrl
        {
            get
            {
                object oIngestUrl;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_PreviewUrl, out oIngestUrl))
                {
                    return (string)oIngestUrl;
                }
                else return "http://channelname.accountname.channel.mediaservices.windows.net/preview.isml/manifest";
            }
            set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_PreviewUrl] = value;
            }
        }

        public string AudioDeviceId
        {
            get
            {
                object oAudioDeviceId;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_AudioDeviceId, out oAudioDeviceId))
                {
                    return (string)oAudioDeviceId;
                }
                else return null;
            }
            set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_AudioDeviceId] = value;
                if (AudioDeviceIdChanged != null) AudioDeviceIdChanged(this, EventArgs.Empty);
            }
        }

        public string VideoDeviceId
        {
            get
            {
                object oVideoDeviceId;
                if (ApplicationData.Current.RoamingSettings.Values.TryGetValue(Key_VideoDeviceId, out oVideoDeviceId))
                {
                    return (string)oVideoDeviceId;
                }
                else return null;
            }
            set
            {
                ApplicationData.Current.RoamingSettings.Values[Key_VideoDeviceId] = value;
                if (VideoDeviceIdChanged != null) VideoDeviceIdChanged(this, EventArgs.Empty);
            }
        }
    }
}
